package airhousePublicApi

import (
	"context"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gctx"
)

var (
	Host         string
	XAHPartner   = "033e644c-b2f1-4f94-ae5a-54b2c09c0488"
	XAccessToken = "0a4c363b46bfd6a96cb09b195ac8c55a"
	LANG         = "ja"
)

func init() {
	var (
		mode      = g.Cfg().MustGet(gctx.New(), "airhousePublicApi.mode").String()
		ConfigMap g.MapStrStr
	)
	if mode == "develop" {
		ConfigMap = g.Cfg().MustGet(gctx.New(), "airhousePublicApi.develop").MapStrStr()
	} else if mode == "produce" {
		ConfigMap = g.Cfg().MustGet(gctx.New(), "airhousePublicApi.produce").MapStrStr()
	} else {
		panic(gerror.New("airhostPublicApi mode error"))
	}
	Host = ConfigMap["host"]
	XAHPartner = ConfigMap["X-AH-Partner"]
	XAccessToken = ConfigMap["X-Access-Token"]

}

// CurlAirHostPublicApiGet get方式请求
func CurlAirHostPublicApiGet(ctx context.Context, path string, params interface{}) (res string, err error, httpStatus int) {
	gclient := g.Client()
	gclient.SetHeader("Accept", "*/*")
	gclient.SetHeader("X-AH-Partner", XAHPartner)
	gclient.SetHeader("X-Access-Token", XAccessToken)
	gclient.SetHeader("Accept-Language", LANG)
	r, err := gclient.Get(ctx, Host+path, params)
	if err != nil {
		return
	}
	defer r.Close()
	httpStatus = r.StatusCode
	g.Log().Path("logs/SDK/AIRHOST_PUBLIC_SDK").Infof(ctx, r.Raw())
	res = r.ReadAllString()
	return
}

// CurlAirHostPublicApiPost post方式请求
func CurlAirHostPublicApiPost(ctx context.Context, path string, params interface{}) (res string, err error, httpStatus int) {
	gclient := g.Client()
	gclient.SetHeader("Accept", "*/*")
	gclient.SetHeader("X-AH-Partner", XAHPartner)
	gclient.SetHeader("X-Access-Token", XAccessToken)
	gclient.SetHeader("Accept-Language", LANG)
	gclient.SetHeader("Content-Type", "application/json")
	r, err := gclient.Post(ctx, Host+path, params)
	if err != nil {
		return
	}
	defer r.Close()
	httpStatus = r.StatusCode
	g.Log().Path("logs/SDK/AIRHOST_PUBLIC_SDK").Infof(ctx, r.Raw())
	res = r.ReadAllString()
	return
}

// CurlAirHostPublicApiPut put方式请求
func CurlAirHostPublicApiPut(ctx context.Context, path string, params interface{}) (res string, err error, httpStatus int) {
	gclient := g.Client()
	gclient.SetHeader("Accept", "*/*")
	gclient.SetHeader("X-AH-Partner", XAHPartner)
	gclient.SetHeader("X-Access-Token", XAccessToken)
	gclient.SetHeader("Accept-Language", LANG)
	r, err := gclient.ContentJson().Put(ctx, Host+path, params)
	if err != nil {
		return
	}
	defer r.Close()
	httpStatus = r.StatusCode
	g.Log().Path("logs/SDK/AIRHOST_PUBLIC_SDK").Infof(ctx, r.Raw())
	res = r.ReadAllString()
	return
}
